package org.yzh.web.config;

import com.ling5821.mtime.Mtime;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * @author lsj
 * @date 2021/1/7 19:09
 */
//@Configuration
public class GsonConfig {

    /*@Bean
    public Gson gsonDateFormatManager() {
        return new GsonBuilder().serializeNulls().setDateFormat(Mtime.DEFAULT_DATE_TIME_PATTERN)
            .registerTypeAdapter(LocalDateTime.class,
                (JsonSerializer<LocalDateTime>)(src, type, jsonSerializationContext) -> new JsonPrimitive(
                    Mtime.format(src))).registerTypeAdapter(LocalDate.class,
                (JsonSerializer<LocalDate>)(src, type, jsonSerializationContext) -> new JsonPrimitive(
                    Mtime.format(src))).registerTypeAdapter(LocalDateTime.class,
                (JsonDeserializer<LocalDateTime>)(jsonElement, type, jsonDeserializationContext) -> Mtime
                    .toLocalDateTime(jsonElement.getAsJsonPrimitive().getAsString()))
            .registerTypeAdapter(LocalDate.class,
                (JsonDeserializer<LocalDate>)(jsonElement, type, jsonDeserializationContext) -> Mtime
                    .toLocalDate(jsonElement.getAsJsonPrimitive().getAsString())).create();
    }

    @Bean
    public Gson gsonTimestampManager() {
        return new GsonBuilder().serializeNulls().setDateFormat(Mtime.DEFAULT_DATE_TIME_PATTERN)
            .registerTypeAdapter(LocalDateTime.class,
                (JsonSerializer<LocalDateTime>)(src, type, jsonSerializationContext) -> new JsonPrimitive(
                    Mtime.toEpochSecond(src))).registerTypeAdapter(LocalDate.class,
                (JsonSerializer<LocalDate>)(src, type, jsonSerializationContext) -> new JsonPrimitive(
                    Mtime.toEpochSecond(src))).registerTypeAdapter(LocalDateTime.class,
                (JsonDeserializer<LocalDateTime>)(jsonElement, type, jsonDeserializationContext) -> Mtime
                    .ofEpochSecond(jsonElement.getAsJsonPrimitive().getAsLong())).registerTypeAdapter(LocalDate.class,
                (JsonDeserializer<LocalDate>)(jsonElement, type, jsonDeserializationContext) -> Mtime
                    .toLocalDateOfEpochMilli(jsonElement.getAsJsonPrimitive().getAsLong())).create();
    }*/
}
